/**
 * @file config.h
 * @brief 客户端配置类 - 基于gRPC的配置
 */

#ifndef KWDB_CONFIG_H
#define KWDB_CONFIG_H

#include <string>
#include <chrono>

namespace kwdb {

/**
 * @brief 客户端配置类
 */
class ClientConfig {
public:
    /**
     * @brief 默认构造函数
     */
    ClientConfig() = default;
    
    /**
     * @brief 构造函数
     * @param host 服务器地址
     * @param port 服务器端口
     */
    ClientConfig(const std::string& host, int port)
        : host_(host), port_(port) {}
    
    // Getters
    const std::string& getHost() const { return host_; }
    int getPort() const { return port_; }
    int getConnectionTimeout() const { return connection_timeout_ms_; }
    int getReadTimeout() const { return read_timeout_ms_; }
    int getWriteTimeout() const { return write_timeout_ms_; }
    int getThreadPoolSize() const { return thread_pool_size_; }
    int getBatchSize() const { return batch_size_; }
    int getBatchTimeout() const { return batch_timeout_ms_; }
    bool isAutoConnect() const { return auto_connect_; }
    int getMaxRetries() const { return max_retries_; }
    int getRetryInterval() const { return retry_interval_ms_; }
    bool isEnableCompression() const { return enable_compression_; }
    bool isEnableTls() const { return enable_tls_; }
    const std::string& getClientId() const { return client_id_; }
    int getMaxInboundMessageSize() const { return max_inbound_message_size_; }
    int getMaxInboundMetadataSize() const { return max_inbound_metadata_size_; }
    bool isKeepAlive() const { return keep_alive_; }
    int getKeepAliveTime() const { return keep_alive_time_; }
    int getKeepAliveTimeout() const { return keep_alive_timeout_; }
    bool isKeepAliveWithoutCalls() const { return keep_alive_without_calls_; }
    
    // Setters (链式调用)
    ClientConfig& setHost(const std::string& host) { host_ = host; return *this; }
    ClientConfig& setPort(int port) { port_ = port; return *this; }
    ClientConfig& setConnectionTimeout(int timeout_ms) { connection_timeout_ms_ = timeout_ms; return *this; }
    ClientConfig& setReadTimeout(int timeout_ms) { read_timeout_ms_ = timeout_ms; return *this; }
    ClientConfig& setWriteTimeout(int timeout_ms) { write_timeout_ms_ = timeout_ms; return *this; }
    ClientConfig& setThreadPoolSize(int size) { thread_pool_size_ = size; return *this; }
    ClientConfig& setBatchSize(int size) { batch_size_ = size; return *this; }
    ClientConfig& setBatchTimeout(int timeout_ms) { batch_timeout_ms_ = timeout_ms; return *this; }
    ClientConfig& setAutoConnect(bool auto_connect) { auto_connect_ = auto_connect; return *this; }
    ClientConfig& setMaxRetries(int retries) { max_retries_ = retries; return *this; }
    ClientConfig& setRetryInterval(int interval_ms) { retry_interval_ms_ = interval_ms; return *this; }
    ClientConfig& setEnableCompression(bool enable) { enable_compression_ = enable; return *this; }
    ClientConfig& setEnableTls(bool enable) { enable_tls_ = enable; return *this; }
    ClientConfig& setClientId(const std::string& client_id) { client_id_ = client_id; return *this; }
    ClientConfig& setMaxInboundMessageSize(int size) { max_inbound_message_size_ = size; return *this; }
    ClientConfig& setMaxInboundMetadataSize(int size) { max_inbound_metadata_size_ = size; return *this; }
    ClientConfig& setKeepAlive(bool enable) { keep_alive_ = enable; return *this; }
    ClientConfig& setKeepAliveTime(int time_sec) { keep_alive_time_ = time_sec; return *this; }
    ClientConfig& setKeepAliveTimeout(int timeout_sec) { keep_alive_timeout_ = timeout_sec; return *this; }
    ClientConfig& setKeepAliveWithoutCalls(bool enable) { keep_alive_without_calls_ = enable; return *this; }

private:
    std::string host_ = "localhost";
    int port_ = 9090;  // gRPC默认端口
    int connection_timeout_ms_ = 5000;
    int read_timeout_ms_ = 10000;
    int write_timeout_ms_ = 10000;
    int thread_pool_size_ = 10;
    int batch_size_ = 1000;
    int batch_timeout_ms_ = 1000;
    bool auto_connect_ = true;
    int max_retries_ = 3;
    int retry_interval_ms_ = 1000;
    bool enable_compression_ = true;
    bool enable_tls_ = false;
    std::string client_id_ = "kwdb-cpp-client-" + std::to_string(std::time(nullptr));
    int max_inbound_message_size_ = 4 * 1024 * 1024; // 4MB
    int max_inbound_metadata_size_ = 8 * 1024; // 8KB
    bool keep_alive_ = true;
    int keep_alive_time_ = 30; // 秒
    int keep_alive_timeout_ = 5; // 秒
    bool keep_alive_without_calls_ = true;
};

} // namespace kwdb

#endif // KWDB_CONFIG_H
